package org.project.product.android.pfe_fanta;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.adapter.DataManagerFacade;
import org.project.product.android.pfe.fanta.domain.Category;
import org.project.product.android.pfe.fanta.domain.User;
import org.project.product.android.pfe.fanta.util.Utils;
import org.projet.product.android.pfe.fanta.barcode.IntentIntegrator;
import org.projet.product.android.pfe.fanta.barcode.IntentResult;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

/**
 * The main class of the application. It is laucnh in firts.
 * 
 * @author JBromo
 * 
 */
public class Home extends Activity {

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		IntentResult scanningResult = IntentIntegrator.parseActivityResult(
		        requestCode, resultCode, data);
		if (scanningResult != null && scanningResult.getContents() != null
		        && scanningResult.getContents().length() > 3) {
			Utils.lancerActivityWithCode(this, AddProduct.class,
			        scanningResult.getContents());
		}
	}

	private void addDefaultCategory() {

		if (DataManagerFacade.getInstance(this).getCategoryFromLocal()
		        .isEmpty()) {

			String[] list = getResources().getStringArray(
			        R.array.default_category);
			Log.i("Category", "item" + list.length);
			for (String item : list) {

				Category category = new Category();
				category.setName(item);
				category.setDescription("Cette categorie contient la famille des "
				        + item);
				DataManagerFacade.getInstance(this).saveCategory(category);
			}
		}
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.accueil);
		addDefaultCategory();

		// Listen the button scan
		ImageView scan = (ImageView) findViewById(R.id.image_scan);
		scan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				launchScanBareCode();
			}
		});

		// Listen the button ean
		ImageView ean = (ImageView) findViewById(R.id.image_ean);
		ean.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Utils.lancerActiviter(v.getContext(), EanSaisir.class);

			}
		});

		// Listen the button panier
		ImageView panier = (ImageView) findViewById(R.id.image_panier);
		panier.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Utils.lancerActiviter(v.getContext(),
				        CategoryListActivity.class);

			}
		});

		// Listen the button panier
		ImageView searchName = (ImageView) findViewById(R.id.image_saisir);
		searchName.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Utils.lancerActiviter(v.getContext(), SearchByProductName.class);

			}
		});
	}

	public void addUser(View v) {
		User user = DataManagerFacade.getInstance(this).getListUserFromLocal();
		if (user != null) {
			Utils.lancerActivity(this, MyCountViewActivity.class, user);
		} else {
			Utils.lancerActivity(this, UserActivity.class);
		}
	}

	private void launchScanBareCode() {
		IntentIntegrator scanIntegrator = new IntentIntegrator(this);
		scanIntegrator.initiateScan();
		// Intent intent = new Intent(this, CaptureActivity.class);
		// intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
		// startActivityForResult(intent, 1212);
	}
}